﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using compshop.web.CompShopDataService;

namespace compshop.web
{
    /// <summary>
    /// ProductList implements adding, deleting good from list by goodID
    /// 
    /// </summary>
    public class ProductList:List<SimpleProduct>
    {
        private void Add()
        {

        }
        public void Add(int id)
        {
            bool found = false;
            for (int i = 0; i < this.Count; i++)
            {
                if (this[i].ProductID == id)
                {
                    this[i].Count++;
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                //ObjectDataSource ods = new ObjectDataSource();
                GeneralProduct gp = ObjectDataSource.GetGeneralProductById(id);
                if (gp != null || gp.CurrentPrice == null)
                {
                    SimpleProduct si = new SimpleProduct();
                    si.ProductID = (int)gp.GeneralProductID;
                    si.Count = 1;
                    si.Price = (double)(gp.CurrentPrice??0);
                    base.Add(si);
                }
            }
        }

        public double GetSum()
        {
            double d = 0;
            foreach (SimpleProduct si in this)
            {
                d += si.Price * si.Count;
            }
            return d;
        }
        public int GetProductCount()
        {
            int c = 0;
            foreach (SimpleProduct si in this)
            {
                c += si.Count;
            }
            return c;
        }
    }
}